
import java.util.logging.Logger;
import src.de.checker.hochzeit.domain.util.*;
import src.de.checker.hochzeit.domain.user.*;
import src.de.checker.hochzeit.konstanten.*
import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*

Logger log = Logger.getLogger('DefChecklisteController');


if(params?.index ==~ /(?i)true/)
{
	index()
}
if(params?.indexkat ==~ /(?i)true/)
{
	indexkat()
}
if(params?.indexprio ==~ /(?i)true/)
{
	indexprio()
}
if(params?.createkat ==~ /(?i)true/)
{
	createkat()
}
if(params?.createcheck ==~ /(?i)true/)
{
	createcheck()
}
if(params?.createprio ==~ /(?i)true/)
{
	createprio()
}
if(params?.deletecheck ==~ /(?i)true/)
{
	deletedefcheck()
}

private void index()
{
	def query = new Query(Konstanten.DEFAULT_CHECKLISTE)
	PreparedQuery preparedQuery = datastore.prepare(query)
 	def entities = preparedQuery.asList( withLimit(300) )
	
 	def query1 = new Query(Konstanten.CKATEGORIE)
	PreparedQuery preparedQuery1 = datastore.prepare(query1)
 	def entities1 = preparedQuery1.asList( withLimit(300) )
 	
 	def query2 = new Query(Konstanten.PRIO)
	PreparedQuery preparedQuery2 = datastore.prepare(query2)
 	def entities2 = preparedQuery2.asList( withLimit(300) )
 	
 	request['prios'] = entities2
 	request['list'] = entities
 	request['kat'] = entities1
 	
 	forward '/admin/checkliste.gtpl'
}
private void indexprio()
{
	def query = new Query(Konstanten.PRIO)
	PreparedQuery preparedQuery = datastore.prepare(query)
 	def entities = preparedQuery.asList( withLimit(300) )
 	
 	request['list'] = entities
 	forward '/admin/checkprio.gtpl'
}
private void indexkat()
{
	def query1 = new Query(Konstanten.CKATEGORIE)
	PreparedQuery preparedQuery1 = datastore.prepare(query1)
 	def entities1 = preparedQuery1.asList( withLimit(300) )
 	
 	request['list'] = entities1
 	
 	forward '/admin/checkkat.gtpl'
}
private void createkat()
{
	Entity entity = new Entity(Konstanten.CKATEGORIE)
	entity['id'] = new Integer(params.id)
	entity['beschreibung'] = params.beschreibung
	entity.save()
	redirect '/admin/defcheckkat'
}
private void createcheck()
{
	Entity entity = new Entity(Konstanten.DEFAULT_CHECKLISTE)
	entity['kurzbeschreibung'] = params.kurzbeschreibung
	entity['anmerkung'] = params.anmerkung
	entity['prio'] = getKey(params.prio,Konstanten.PRIO)
	entity['kategorie'] = getKey(params.kategorie,Konstanten.CKATEGORIE)
	entity.save()
	redirect '/admin/defcheck'
}
private void createprio()
{
	Entity entity = new Entity(Konstanten.PRIO)
	entity['id'] = new Integer(params.id)
	entity['beschreibung'] = params.beschreibung
	entity.save()
	redirect '/admin/defcheckprio'
}
private void deletedefcheck()
{
	Key item = KeyFactory.stringToKey(params.key)
	log.info("Lösche Default-Checklisten Eintrag: "+item)
	item.delete()
	redirect '/admin/defcheck'
}
private Key getKey(String id, String kind)
{
	long mid = new Long(id).longValue()
	Key k = KeyFactory.createKey(kind,mid)
	return k
}